package com.lanqiao.guimei.listener;

import com.lanqiao.guimei.commons.CustomDateTimeConverter;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 *
 * 使用监听器注册BeanUtils中日期转换类
 * @author 李尚林 on 2019/8/15 9:31
 * @85182
 */
@WebListener
public class DateConverterListener implements ServletContextListener {

    static Logger logger = Logger.getLogger(DateConverterListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        logger.info("初始化toDate监听器，注册beanUtils的转换日期的格式");

        CustomDateTimeConverter dateTimeConverter = new CustomDateTimeConverter();
        ConvertUtils.register(dateTimeConverter, java.util.Date.class);
        ConvertUtils.register(dateTimeConverter, java.sql.Date.class);
        //ConvertUtils.register(dateTimeConverter, java.sql.Timestamp.class);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        logger.info("销毁监听器");
    }
}
